package com.xinghou.platform.dao.main.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.dao.main.UserRelationFromDAO;
import com.xinghou.platform.model.main.UserRelationFrom;

/**
 * 
 * @author wzk
 *
 */
@Service
public class UserRelationFromDAOImpl {/*
	@Resource
	private JdbcTemplate jdbcTemplate;

	@Override
	public UserRelationFrom getUserRelationFromById(Integer Id)
			throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT Id, From_user_id, To_user_id, Is_liked, Is_blocked, Create_time FROM user_relation_from WHERE Id = ?";
		UserRelationFrom userrelationfrom = jdbcTemplate.queryForObject(sql, new Object[] { Id }, new UserRelationFromMapper());
		CustomerContextHolder.clearCustomerType();
		return userrelationfrom;
	}

	@Override
	public int updateUserRelationFrom(UserRelationFrom userRelationFrom)
			throws DataAccessException {
		StringBuffer strSQL = new StringBuffer(
				"UPDATE user_relation_from SET From_user_id=?,To_user_id=?,Is_liked=?,Is_blocked=?,Create_time=? WHERE Id=?"	
				);
		Object[] params = {userRelationFrom.getFromUserId(),userRelationFrom.getToUserId(),userRelationFrom.getIsLiked(),
				userRelationFrom.getIsBlocked(),userRelationFrom.getCreateTime(),userRelationFrom.getId()
		};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret = jdbcTemplate.update(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}

	@Override
	public int saveUserRelationFrom(UserRelationFrom userRelationFrom)
			throws DataAccessException {
		StringBuffer strSQL = new StringBuffer(
				"INSERT INTO user_relation_from (From_user_id,To_user_id,Is_liked,Is_blocked,Create_time) VALUES (?,?,?,?,?);"
				);
		Object[] params = {userRelationFrom.getFromUserId(),userRelationFrom.getToUserId(),userRelationFrom.getIsLiked(),
				userRelationFrom.getIsBlocked(),userRelationFrom.getCreateTime()
		};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret =jdbcTemplate.save(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}
	
	@Override
	public UserRelationFrom getUserRelationFromByUserId(Integer fromUserId,Integer toUserId) {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT Id, From_user_id, To_user_id, Is_liked, Is_blocked, Create_time FROM user_relation_from WHERE From_user_id = ? AND To_user_id=?";
		UserRelationFrom userrelationfrom = jdbcTemplate.queryForObject(sql, new Object[] { fromUserId,toUserId }, new UserRelationFromMapper());
		CustomerContextHolder.clearCustomerType();
		return userrelationfrom;
	}

	@Override
	public List<UserRelationFrom> getUserRelationFromByToUserId(Integer fromUserId) {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT Id, From_user_id, To_user_id, Is_liked, Is_blocked, Create_time FROM user_relation_from WHERE From_user_id = ?";
		List<UserRelationFrom> list = jdbcTemplate.query(sql, new Object[] { fromUserId}, new UserRelationFromMapper());
		CustomerContextHolder.clearCustomerType();
		return list;
	}

	@Override
	public List<UserRelationFrom> getUserRelationFromList(int fromUserId,
			Timestamp id, int rowNum) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT Id, From_user_id, To_user_id, Is_liked, Is_blocked, Create_time FROM user_relation_from WHERE From_user_id = ? AND Is_liked=1 AND Create_time<? ORDER BY Create_time DESC LIMIT ? ";
		List<UserRelationFrom> list = jdbcTemplate.query(sql, new Object[] { fromUserId,id,rowNum}, new UserRelationFromMapper());
		CustomerContextHolder.clearCustomerType();
		return list;
	}
	
	private static final class UserRelationFromMapper implements RowMapper<UserRelationFrom> {
		public UserRelationFrom mapRow(ResultSet rs, int rowNum) throws SQLException {
			UserRelationFrom userrelationfrom = new UserRelationFrom();
			userrelationfrom.setId(rs.getInt("Id"));
			userrelationfrom.setFromUserId(rs.getInt("From_user_id"));
			userrelationfrom.setToUserId(rs.getInt("To_user_id"));
			userrelationfrom.setIsLiked(rs.getShort("Is_liked"));
			userrelationfrom.setIsBlocked(rs.getShort("Is_blocked"));
			userrelationfrom.setCreateTime(rs.getTimestamp("Create_time"));
			return userrelationfrom;
		}
	}*/



}
